//
//  GHHomeViewController.swift
//  Browser
//
//  Created by 刘光华 on 16/4/27.
//  Copyright © 2016年 liuguanghua. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class GHHomeViewController: UICollectionViewController {

    // MARK:- 懒加载
    lazy var shops: [GHShop] = [GHShop]()
    
    lazy var photoBrowserAnimator = GHPhotoBrowserAnimator()
    
    // MARK:- 系统回调方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadData(0)
        
    }

}

// MARK:- 发送网络请求
extension GHHomeViewController {
    
    func loadData(offset: Int){

        GHNetWorkTool.shareInstance.loadHomeData(offset) { (result, error) -> () in
            
            // 空值校验
            guard error == nil else {
                print(error)
                return
            }
            
            // 数据校验
            guard let result = result else {
                print("数据错误")
                return
            }
            
            // 字典转模型
            for resultDict in result {
                self.shops.append(GHShop.init(dict: resultDict))
            }
            
            // 刷新列表
            self.collectionView?.reloadData()
        }
        
    }
    
}
// MARK:- 自定义函数
extension GHHomeViewController {
    
    /// 显示图片浏览器
    func showPhotoBrowserController(indexPath: NSIndexPath) {
        
        let photoBrowserController = GHPhotoBrowserController()
        
        photoBrowserController.indexPath = indexPath
        photoBrowserController.shops = self.shops
        
        // 设置modal样式
        photoBrowserController.modalPresentationStyle = .Custom
        photoBrowserController.transitioningDelegate = photoBrowserAnimator
        
        // 设置动画代理
        photoBrowserAnimator.indexPath = indexPath
        photoBrowserAnimator.presentDelegate = self
        photoBrowserAnimator.dismissDelegate = photoBrowserController
        
        presentViewController(photoBrowserController, animated: true, completion: nil)
        
    }
    
}

// MARK:- 弹出动画代理

extension GHHomeViewController: GHPresentDelegate {
    
    /// 返回imageView
    func imageView(indexPath: NSIndexPath) -> UIImageView {
        
        let imageView = UIImageView()
        let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! GHShopCell
        let image = cell.imageView.image
        
        imageView.image = image
        imageView.contentMode = .ScaleAspectFill
        
        
        return imageView
    }
    
    /// 计算imageView的最终位置
    func imageViewFinalPosition(indexPath: NSIndexPath) -> CGRect {
        let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! GHShopCell
        
        
        let image = cell.imageView.image!
        
        return calculateImageViewHeight(image)
    }
    
    /// 计算imageView的起始位置
    func imageViewHomePosition(indexPath: NSIndexPath) -> CGRect {
        
        // 获取当前的cell
        let cell = collectionView?.cellForItemAtIndexPath(indexPath) as? GHShopCell
        
        // 坐标转换
        
        guard let frame = cell?.frame else {
            
            return CGRect(x: (UIScreen.mainScreen().bounds.size.width - 100) * 0.5, y: UIScreen.mainScreen().bounds.size.height, width: 100, height: 100)
            
        }
        
        let homePosition = collectionView!.convertRect(frame, toView: UIApplication.sharedApplication().keyWindow)
        
        return homePosition
        
    }
    
}


// MARK:- UICollectionViewDatasource and UICollectionViewDelegate
extension GHHomeViewController {
    /// UICollectionViewDatasource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return shops.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("shopId", forIndexPath: indexPath) as! GHShopCell
        cell.shop = shops[indexPath.item]
        
        // 当显示到最后时自动加载更多
        if indexPath.item == shops.count - 1 {
            loadData(shops.count)
        }
        
        return cell
    }
    
    /// UICollectionViewDelegate
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        // 展示图片浏览器
        showPhotoBrowserController(indexPath)
        
    }
}

